home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 0789 / ncsa2net.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-04-10  |  5.9 KB  |  209 lines

  1. /*****************************************************************************/
  2. /* Program name:                  NCSA2NET.C                                 */
  3. /*****************************************************************************/
  4. /* Programmer: Kevin Georgison                                               */
  5. /* Date:       Aug 27, 1995                                                  */
  6. /*****************************************************************************/
  7. /* Program Description:    Convert NCSA hot files into a format that Netscape   */
  8. /*    can import.                                                          */
  9. /*                                                                           */
  10. /*****************************************************************************/
  11. /* Comments: Modified by Jack Fitts to convert NCSA mosaic 2.0 hotlist with  */
  12. /*    time/date stamps to Netscape import fomat.                           */
  13. /*****************************************************************************/
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. #define MAX_L_LEN 512
  20. #define MAX_MENUS 64
  21.  
  22. int menu_search(int cur_menu_num, FILE *fs, char *ln);
  23. int get_menu_num(char *ln);
  24. void get_title(char *ln, char *menu_name);
  25. void out_title(char *menu_name);
  26. int get_item(FILE *fs, char *ln);
  27. char test_item(char *ln);
  28. void out_item(char *url, char *title);
  29. void parse_item(char *ln, char *title, char *url);
  30. int isitnew(int *done_menus, int cur_menu_num);
  31. void initialize(int *done_menus);
  32. int get_submenu_num(char *ln);
  33.  
  34. void main(int argc, char **argv) {
  35.     FILE    *fs,
  36.         *fd;
  37.     char    ln[MAX_L_LEN],
  38.         url[MAX_L_LEN],
  39.         title[MAX_L_LEN],
  40.         menu_name[MAX_L_LEN],
  41.         i_type;
  42.     int    done_menus[MAX_MENUS],
  43.         menutreelevel=0,
  44.         here_again=0,
  45.         submenu_num,
  46.         first_search=1,
  47.         failed_a_prev_read=0,
  48.         cur_menu_num=0;
  49.     long    fp[MAX_MENUS],
  50.         prev_line;
  51.  
  52.         if(argc == 1) {
  53.             printf("USAGE:\n\tncsa2net file.hot > newfile.htm\n\n"
  54.                 "\tConvert a NCSA Mosaic HOT list to a NETSCAPE bookmark HTML\n\n"
  55.                 "\n\tSorry!  This program is NOT a filter. "
  56.                 "It can not accept input\n\tfrom a pipe.\n\n"
  57.                 "\tThis is FreeWare written by Kevin Georgison\n"
  58.                 "\tE-Mail comments to: ljs263@freenet.mb.ca\n\t\t\t\tor\n"
  59.                 "\tgeorgkev@mbnet.mb.ca, http://www.mbnet.mb.ca/~georgkev"
  60.                 "\n");
  61.             exit(0);
  62.         }
  63.         if((fs = fopen(argv[1], "r")) == NULL) {
  64.             printf("cannot open file %s\n", argv[1]);
  65.             exit(1);
  66.         }
  67.         initialize(done_menus);
  68.  
  69.         out_title("NCSA Mosaic URL's");
  70.         do {
  71.             if(!get_item(fs, ln) ) {
  72.                   if(!failed_a_prev_read || !first_search) printf("</DL><p>\r\n");
  73.                   failed_a_prev_read=1;
  74.                   if(menutreelevel) {
  75.                 fseek(fs, fp[--menutreelevel], SEEK_SET);
  76.                 get_item(fs, ln);
  77.                   }
  78.                   else break;
  79.             }
  80.             i_type = test_item(ln);
  81.             switch(i_type) {
  82.                 case 'i':
  83.                     parse_item(ln, title, url);
  84.                     out_item(url, title);
  85.                     break;
  86.                 case 'n':
  87.                     if(menutreelevel) {
  88.                         printf("</DL><p>\r\n");
  89.                         fseek(fs, fp[--menutreelevel], SEEK_SET);
  90.                         break;
  91.                     }
  92.                     cur_menu_num = get_menu_num(ln);
  93.                     if(!isitnew(done_menus, cur_menu_num))
  94.                     {    do {
  95.                           prev_line = ftell(fs);
  96.                           if(!get_item(fs, ln)) break;
  97.                         }while(*ln != '[');
  98.                         fseek(fs, prev_line, SEEK_SET);
  99.                         if(first_search) {
  100.                           printf("</DL><p>\r\n");
  101.                           first_search=0;
  102.                         }
  103.                         break;
  104.                     }
  105.                     first_search=1;
  106.                     if(here_again) printf("</DL><p>\r\n");
  107.                     here_again=1;
  108.                     done_menus[cur_menu_num]=cur_menu_num;
  109.                     get_item(fs, ln);
  110.                     get_title(ln, menu_name);
  111.                     out_title(menu_name);
  112.                     break;
  113.                 case 's':
  114.                     fp[menutreelevel++] = ftell(fs);
  115.                     submenu_num=get_submenu_num(ln);
  116.                     rewind(fs);
  117.                     menu_search(submenu_num, fs, ln);
  118.                     done_menus[submenu_num]=submenu_num;
  119.                     get_item(fs, ln);
  120.                     get_title(ln, menu_name);
  121.                     out_title(menu_name);
  122.                     break;
  123.             }
  124.         } while (1);
  125. }
  126. int menu_search(int cur_menu_num, FILE *fs, char *ln) {
  127.     int n_found;
  128.  
  129.     while (*ln != '[') {
  130.         if( (fgets(ln, MAX_L_LEN, fs)) == NULL)    return(0);
  131.     }
  132.     n_found = get_menu_num(ln);
  133.     if(n_found != cur_menu_num) {
  134.         get_item(fs, ln);
  135.         n_found = menu_search(cur_menu_num, fs, ln);
  136.     }
  137.     return(n_found);
  138. }
  139. int get_menu_num(char *ln) {
  140.     int menu_num;
  141.     menu_num = atoi(&ln[10]);
  142.     return (menu_num);
  143. }
  144. void get_title(char *ln, char *menu_name) {
  145.     strcpy(menu_name, &ln[10]);
  146.     menu_name[strlen(menu_name)-1]='\0';
  147.     return;
  148. }
  149. void out_title(char *menu_name) {
  150.     printf("<DT><H3 ADD_DATE=\"808383052\">%s"
  151.         "</H3>\r\n<DL><p>\r\n", menu_name);
  152.     return;
  153. }
  154. int get_item(FILE *fs, char *ln) {
  155.     if (fgets(ln, MAX_L_LEN, fs) == NULL) return(0);
  156.     return(1);
  157. }
  158. char test_item(char *ln) {
  159.     char junkstr[MAX_L_LEN], *p;
  160.  
  161.     strcpy(junkstr, ln);
  162.     switch (*junkstr) {
  163.         case '[':    return('n');
  164.         case 'I':    strtok(junkstr, "=");
  165.                 p=strtok(NULL, ",");
  166.                 if(!strcmp(p, "MENU")) return('s');
  167.                 else return('i');
  168.     }
  169.     return('\0');
  170. }
  171. void parse_item(char *ln, char *title, char *url) {
  172.     char junkstr[MAX_L_LEN], *p;
  173.  
  174.     strcpy(junkstr, ln);
  175.     strtok(junkstr, "=");
  176.     p=strtok(NULL, ",");
  177.     strcpy(title, p);
  178.     p=strtok(NULL,"\n");            /* MOSAIC 1.x    */
  179.     if(p == NULL) p=strtok(NULL, ",");    /* MOSAIC2.x    */
  180.     strcpy(url, p);
  181.     return;
  182. }
  183. void out_item(char *url, char *title) {
  184.     printf("<DT><A HREF=\"%s\" ADD_DATE=\"808302856\" "
  185.         "LAST_VISIT=\"0\">%s</A>\r\n", url, title);
  186.     return;
  187. }
  188. int isitnew(int *done_menus, int cur_menu_num) {
  189.     int n;
  190.  
  191.     for(n=0; done_menus[n] != cur_menu_num && n != MAX_MENUS; ++n);
  192.     if(n == MAX_MENUS) return(1);
  193.     return(0);
  194. }
  195. void initialize(int *done_menus) {
  196.     int mtl;
  197.     for(mtl=0; mtl != MAX_MENUS; ++mtl)
  198.     { done_menus[mtl]=MAX_MENUS; }
  199.     return;
  200. }
  201. int get_submenu_num(char *ln) {
  202.     char *p, str[MAX_L_LEN];
  203.  
  204.     strcpy(str, ln);
  205.     strtok(str, "u");
  206.     p=strtok(NULL, "\0");
  207.     return(atoi(p));
  208. }
  209.